﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Text;
using System.Collections;
using Common.Logging;

namespace Efficient.DataAccess
{
    internal class QueryParameter : IQueryParameter
    {
        private readonly IDbService dao;
        private object _value;
        private bool isMutilValue = false;
        private string name;
        private ParameterDirection direction = ParameterDirection.Input;
        private readonly IList values = new ArrayList();
        private static readonly ILog logger = LogManager.GetLogger(typeof(QueryParameter));
        const string MUTIL_PARAM = "$Name$_$Index$__";

        public QueryParameter(IDbService dao)
        {
            this.dao = dao;
        }

        public ParameterDirection Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        public string Name
        {
            set { name = value; }
            get { return name; }
        }

        public object Value
        {
            set { _value = value; }
            get { return _value; }
        }

        public bool IsMutilValue
        {
            set { isMutilValue = value; }
            get { return isMutilValue; }
        }

        public void SetValues(IEnumerable valueSet)
        {
            this.values.Clear();
            foreach (object item in valueSet)
            {
                this.values.Add(item);
            }
        }

        #region IParameter Members


        public IDbDataParameter[] ToDbParameter(ref string rawQuery)
        {
            if (this.dao == null)
            {
                throw new NullReferenceException("请设置DAO");
            }
            List<IDbDataParameter> result = new List<IDbDataParameter>();
            if (rawQuery.IndexOf(this.name) == -1) //如果语句没有包含这个参数则返回
            {
                if(logger.IsWarnEnabled)
                {
                    logger.Warn(string.Format("语句:{0},并没有包含参数:{1}", rawQuery, this.name));
                }
                return new IDbDataParameter[0];
            }
            if (isMutilValue)
            {
                StringBuilder param = new StringBuilder();
                StringBuilder paramString = new StringBuilder();
                int count = this.values.Count;
                for (int i = 0; i < count; i++)
                {
                    param.Append(MUTIL_PARAM);
                    param.Replace("$Name$", this.name);
                    param.Replace("$Index$", i.ToString());
                    IDbDataParameter p = dao.CreateParameter(param.ToString(), ParameterDirection.Input, this.values[i]);
                    result.Add(p);

                    paramString.Append(param.ToString());
                    if (i < count - 1)
                    {
                        paramString.Append(", ");
                    }
                    param.Remove(0, param.Length);
                }
                if (paramString.Length > 0)
                {
                    rawQuery = rawQuery.Replace(this.name, paramString.ToString());
                }
            }
            else
            {
                IDbDataParameter p = dao.CreateParameter(this.name, ParameterDirection.Input, _value);
                result.Add(p);
            }
            return result.ToArray();
        }


        public IDbDataParameter ToDbDataParameter(IDbService dao)
        {
            if (!isMutilValue)
            {
                return dao.CreateParameter(this.name, this.direction, this._value);
            }
            throw new NotSupportedException("不支持！请调用ToDbParameter");
        }

        #endregion
    }
}